%% Lab 1
x_axis = [];
output;

rows = cell(1,max(time_data(:,end)))

for iteration = 1:max(time_data(:,end))
    
    
for j=1:length(time_data(:,1))
        
    if (time_data(j,end) == iteration)
        rows{iteration} = [rows{iteration} j];
    end
    
end
end

global_times = zeros(max(time_data(:,end)),9);

for iteration = 1:max(time_data(:,end))
    row = rows{iteration};
       
    % Global time
    x_axis = [];
    row_index = [1 2 3 5 8 12 17 23 30];
for i=1:9
    x_axis = [x_axis i-1];
    global_times(iteration,i) = global_times(iteration,i) + time_difference_global(time_data(row(row_index(i)),:));
end

end


for i=2:8
    tmp=time_data(find((time_data(:,1)==i)==1),:);
    thread_time = (1:length(tmp(:,1)))*0;
    thread_time_std = (1:length(tmp(:,1)))*0;
    for j = 1:length(tmp(:,1))
        thread_time(j) = thread_time(j) + time_difference_thread(tmp(j,:));
    end
    
    for k=1:i
    thread_time(k) = mean(thread_time(k:i:end));
    thread_time_std(k) = std(thread_time(k:i:end));
    end
    % Time per thread
    figure(2);
    subplot(4,2,i-1);
    bar(thread_time(1:i));
    title(['# of threads = ' num2str(i)])
    xlabel('Thread ID');
    ylabel('time (ms)');
    ymax = 1.4*max(thread_time(1:i));
    axis([0 i+1 0 ymax]);
    hold on;
    errorbar(1:i,thread_time(1:i),thread_time_std(1:i),'r.','linewidth',1);
    hold off
end

global_times_mean = mean(global_times,1)
global_times_std = std(global_times,1)


figure(1);
plot(x_axis,global_times_mean,'-o', 'linewidth',2)
axis([-0.1 max(x_axis)+1 0 1.2*max(global_times_mean)]) 
xlabel('# of threads');
ylabel('time (ms)');
hold on
errorbar(0:8,global_times_mean,global_times_std,'r.','linewidth',1);
hold off
  
